package com.ruoyi.framework.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.PostConstruct;

/**
 * Swagger 默认配置
 *
 * @author jakgong
 * @version V1.0
 * @date 2020/6/27
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 101)
@Import(BeanValidatorPluginsConfiguration.class)
@ConditionalOnProperty(prefix = "app.swagger", name = "enabled", havingValue = "true")
public class SwaggerAutoConfiguration implements WebMvcConfigurer {
    @PostConstruct
    public void init() {
        log.info("\nWeb starter -- 开启了 Swagger 文档支持\n 开始swagger登录验证\n 登录用户名：admin \n 登录密码：123456");
    }

    /**
     * 必须加，否则无法访问 /doc.html
     * 显示swagger-ui.html文档展示页，还必须注入swagger资源：
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
